package com.winchain.chainbill.tbill.biz.rx;

import com.shell.bee.acct.account.po.AcctBas;
import com.shell.bee.acct.account.po.AcctBkp;
import com.shell.bee.acct.account.util.VirtualAcctUtil;
import com.shell.bee.acct.transaction.po.AcctTxRec;
import com.shell.bee.base.utils.DateUtil;
import com.shell.bee.base.utils.SpringContextUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.constant.rel.AssetCode;
import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.winchain.chainbill.ChainBillUtil;
import com.winchain.chainbill.tbill.base.po.TbDct;
import com.winchain.chainbill.tbill.base.po.TbRec;
import com.winchain.chainbill.tbill.biz.TBill;
import com.winchain.chainbill.tbill.biz.TBillEvent;
import com.winchain.chainbill.trade.service.BillAcctBlcService;
import com.winchain.chainbill.trade.service.BillAcctService;
import com.winchain.chainbill.trade.service.TradeService;
import java.math.BigDecimal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class TBillDctPayTranSubscriber extends OnErrorNotImplementedSubscriber<TBillEvent<TbDct>> {
	@Autowired
	private BillAcctService billAcctService;
	@Autowired
	private BillAcctBlcService billAcctBlcService;
	@Autowired
	private TradeService tradeService;

	public void onApplicationEvent(TBillEvent<TbDct> t) {
		TbRec rec = t.getBill().getRec();
		TbDct dct = (TbDct) t.getBill().getSource();

		AcctTxRec tran = getTran(rec, dct);
		this.tradeService.callback(tran);
	}

	private AcctTxRec getTran(TbRec rec, TbDct dct) {
		String grpId = rec.getGrpId();
		if ((!ChainBillUtil.onlinePay()) || (!VirtualAcctUtil.onVirtual())) {
			grpId = null;
		}
		BigDecimal txAmt = dct.getDctAmt();
		if (dct.getBuyerId().equals(dct.getSellerId())) {
			txAmt = txAmt.subtract(dct.getDctInt());
			if (dct.getDeductInt() != null) {
				txAmt = txAmt.add(dct.getDeductInt());
			}
		}
		AcctTxRec txRec = new AcctTxRec();
		txRec.setTxId(SystemUtil.getUUID());
		txRec.setTxSn(txRec.getTxId());
		txRec.setTxAmt(txAmt);
		txRec.setTxDt(DateUtil.getSystemDate());
		txRec.setTxTs(DateUtil.getSystemDateTime());
		txRec.setTxAbs("��������-������������");
		txRec.setTxType("dct_pay");

		String sourceCode = SpringContextUtil.getProperty("system.code");
		txRec.setSrcCode(sourceCode);
		txRec.setBizId(dct.getBillId());
		txRec.setDataScLvl("B");
		txRec.setTxCashFlag("N");
		txRec.setTxReqFiCls("1");
		txRec.setTxOperator("System");
		txRec.setCurrCode(AssetCode.CNY.name());
		txRec.setTxChnCode("B");

		txRec.setOptType("qt_trd");

		txRec.setTxSts("I");

		AcctBas txAcct = this.billAcctService.getAcctBas(dct.getFinId(), null, "00");
		txRec.setTxAcctNo(txAcct.getAcctNo());
		txRec.setTxAcctNm(txAcct.getAcctNm());
		txRec.setTxBankNm(txAcct.getFiNm());

		AcctBas cpAcct = this.billAcctService.getAcctBas(dct.getSellerId(), null, "00");
		txRec.setCpAcctNo(cpAcct.getAcctNo());
		txRec.setCpAcctNm(cpAcct.getAcctNm());
		txRec.setCpBankNm(cpAcct.getFiNm());

		AcctBkp txAcctBkp = this.billAcctBlcService.getAcctBkp(txRec.getTxAcctNo(), txRec.getCurrCode(), grpId, null);
		txRec.setTxBkpId(txAcctBkp.getBkpId());
		AcctBkp cpAcctBkp = this.billAcctBlcService.getAcctBkp(txRec.getCpAcctNo(), txRec.getCurrCode(), grpId, null);
		txRec.setCpBkpId(cpAcctBkp.getBkpId());
		return txRec;
	}
}
